// 경로 계산
function calcRoute() {
	var directionsService = new google.maps.DirectionsService();
	var start = document.getElementById('start').value;
	var end = document.getElementById('end').value;
	var time = new Date();
	var stime = sessionStorage["time"].split(":");
	time.setHours(stime[0], stime[1], 0, 0);
	var request = {
		origin : start,
		destination : end,
		travelMode : google.maps.TravelMode.TRANSIT,
		transitOptions : {
			departureTime : time
		},
		provideRouteAlternatives : true
	};
	directionsService.route(request, function(response, status) {		
		if (status == google.maps.DirectionsStatus.OK) {
			sessionStorage["req"] = JSON.stringify(response);
			
			// <div class="list-case">
			var t = '';
			for (var j = 0; j < response.routes.length; j++) {
				(0,10);
				t += '<form name="sr-to-ap" role="form" method="post" action="alarm-page.jsp">';
				t += 	'<input type="hidden" name="journey" value="' + time.getTime() + '' + j + '">';
				t += 		'<div class="panel panel-default list_box_style">';
				t += 			'<div class="list-header">';
				t +=				'<div class="list-top">';
				t += 					'<span class="label label-info">경로 ' + (j+1) + '</span>';
				t +=					'<span class="text-danger top-text-style"> 소요시간: <strong>' + Math.ceil(response.routes[j].legs[0].duration.value / 60) + '분</strong></span>';
				t +=					'<span class="top-text-style">, <strong>도착예정: ' + response.routes[j].legs[0].arrival_time.text + '</strong></span>';
				t +=				'</div>';
				t += 			'</div>';
				t += 			'<div class="panel-heading">';
				t +=				'<h4 class="panel-title">';
				t +=					'<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse' + j + '">';
				t += 						'<div class="list">'; 	 /*
																 * (' +
																 * response.routes[j].legs[0].start_address + ') -> ';
																 */
				for (var i = 0; i < response.routes[j].legs[0].steps.length; i++) {
					// var min =
					// Math.ceil(response.routes[j].legs[0].steps[i].duration.value
					// / 60);
					// t += ' / 약 ' + min + ' 분';
					if (response.routes[j].legs[0].steps[i].transit != null) {
						// t += ' / ';
						// t +=
						// response.routes[j].legs[0].steps[i].transit.line.name;
						// t += ' / ';
						//t +=				'<span class="label label-primary step_style">스탭' + (i+1) + '</span>';
						t += 					'<img class="icon_style" style="height: 20px; width: 20px" src="' + response.routes[j].legs[0].steps[i].transit.line.vehicle.icon + '">';
						if (response.routes[j].legs[0].steps[i].transit.line.short_name != null) {
							t += 				'<span style="background-color:' + response.routes[j].legs[0].steps[i].transit.line.color 
												+ ';padding:0 4px;color:#ffffff">'
												+ response.routes[j].legs[0].steps[i].transit.line.short_name + '</span>';
							t += 				response.routes[j].legs[0].steps[i].transit.departure_stop.name;
						} else if (response.routes[j].legs[0].steps[i].transit.line.short_name == null) {
							t += 				'<span style="background-color:'
												+ '#d24999' 
												+ ';padding:0 4px;color:#ffffff">'
												+ response.routes[j].legs[0].steps[i].transit.line.name
												+ '</span>';
						}
						t += 					' <i class="fa fa-caret-right"></i> ';
						t +=				 response.routes[j].legs[0].steps[i].transit.arrival_stop.name;
						var dura = Math.ceil(response.routes[j].legs[0].steps[i].duration.value / 60);
						t += 					'<span class="text-muted">[약 '+dura+'분]</span>';
						
						if (i != ((response.routes[j].legs[0].steps.length) - 1)) {
							t += 				'<br /><br />';
						} 
						// t +=
						// response.routes[j].legs[0].steps[i].transit.line.vehicle.type;
					} else {
						//t +=				'<span class="label label-primary step_style">스탭' + (i+1) + '</span>';
						t += 					'<img class="icon_style" style="height: 20px; width: 20px" src="https://maps.gstatic.com/mapfiles/transit/iw/6/walk.png">';	
						
						if (i == ((response.routes[j].legs[0].steps.length) - 1)) {
							t += sessionStorage["el"];
							t += '<span class="text">까지 도보</span>';
						}else {
							t += 					response.routes[j].legs[0].steps[i].instructions;
						}
						
						var dura = Math.ceil(response.routes[j].legs[0].steps[i].duration.value / 60);
						t += 					'<span class="text-muted">[약 '+dura+'분]</span>';
						if (i != ((response.routes[j].legs[0].steps.length) - 1)) {
							t += 				'<br /><br />';
						}
					}
				};
				t += '</div></a></h4></div>';
				t += '<div id="collapse' + j
						+ '" class="panel-collapse collapse">';
				t += '<div class="panel-body">';
				t += '<div class="pull-right"><button class="list-btn1 btn btn-info" ';
				t += 'onclick="location.href = ';
				t += '\'journey.jsp?';
				t += 'start=' + start;
				t += '&end=' + end;
				t += '&time=' + time.getTime();
				t += '&journey=' + j;
				t += '\';return false;" value="'+ j +'">지도보기</button>';
				t += '<button id="rselect" class="list-btn2 btn btn-warning margin-left-10">경로선택</button></div>';
				t += '</div></div></div></form>';
			};
			$('#test').html(t);
		} else if (status == google.maps.DirectionsStatus.NOT_FOUND) {
			alert('출발지 또는 도착지의 GPS값을 찾을 수 없습니다. 검색(돋보기)버튼으로 위치를 찾아 지정해 주세요.');
			histroy.back();
		} else if (status == google.maps.DirectionsStatus.ZERO_RESULTS) {
			alert('경로를 찾을 수 없습니다. 출발지와 도착지를 확인해 주세요');
			histroy.back();
		} else if (status == google.maps.DirectionsStatus.INVALID_REQUEST) {
			alert('출발지 혹은 도착지가 누락되었습니다.');
			histroy.back();
		} else if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT) {
			alert('데이터 허용치를 초과하였습니다.');
			histroy.back();
		} else if (status == google.maps.DirectionsStatus.REQUEST_DENIED) {
			alert('서버에 오류가 발생하였습니다. 다시 시도해 주세요.');
			histroy.back();
		} else if (status == google.maps.DirectionsStatus.UNKNOWN_ERROR) {
			alert('서버에 오류가 발생하였습니다. 다시 시도해 주세요.');
			histroy.back();
		} 
	});
}